package com.tian.controller.user;

import com.tian.common.exception.BusinessException;
import com.tian.common.util.CommonResult;
import com.tian.dto.user.AuthLoginDto;
import com.tian.service.user.AbstractLoginService;
import com.tian.service.user.LoginService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * {@code @description:}  用户登录token获取和验证
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-04-07 16:42
 * {@code @version:} 1.0
 */
@RestController
@RequestMapping("/auth/user")
public class LoginController {

    @PostMapping("/login")
    public CommonResult<String> getToken(@RequestBody AuthLoginDto authLoginDto, BindingResult bindingResult) {
        authLoginDto.validData(bindingResult);
        LoginService login = AbstractLoginService.LOGIN_TYPE_MAP.get(authLoginDto.getLoginType());
        if (login == null) {
            throw new BusinessException("暂不支持该种登录类型");
        }
        return login.doLogin(authLoginDto);
    }
}
